<template>
  <el-row v-loading="loading">

    <el-col :span="24">
           <assess-edit ref="assessEdit" v-on:save-finished="getAssessList()"></assess-edit>
      <el-row class="search-row">
        <el-col :span="12">
          <el-input placeholder="姓名/会员编号/手机号" size="small" v-model="search.name">
            <el-button slot="append" icon="el-icon-search" @click="getAssessList()"></el-button>
          </el-input>
        </el-col>
               <el-col :span="12" style="text-align: right;">
                 <el-button type="primary" icon="el-icon-plus" size="small"
                            v-show="permission.add" @click="handleCreateAssess()">添加</el-button>
               </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-table :data="assessList" border size="mini" header-cell-class-name="header-row" style="text-align : center;">
            <el-table-column prop="memberId" label="档案号" header-align="center"></el-table-column>
            <el-table-column prop="tMember.name" label="姓名" header-align="center"></el-table-column>
            <el-table-column prop="time" label="评估时间" header-align="center"></el-table-column>
            <el-table-column prop="operator" label="操作人" header-align="center"></el-table-column>
            <el-table-column prop="state" label="报告状态" header-align="center">
              <template slot-scope="scope">
                <span v-if="scope.row.state ==0 ">未出报告</span>
                <span v-else >已出报告</span>
              </template>
            </el-table-column>
            <el-table-column label="操作" header-align="center" align="center" width="260">
                           <template slot-scope="scope">
                             <el-button v-show="permission.edit" type="text"
                                        @click="handleEditAssess(scope.$index, scope.row)">编辑</el-button>
                             <el-button v-show="permission.edit" type="text"
                                        @click="handleEditAssess(scope.$index, scope.row)">查看</el-button>
                           </template>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="24">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="search.currentPage"
            :page-sizes="[5, 10, 20]"
            :page-size="5"
            layout="total, sizes, prev, pager, next, jumper"
            :total="assessTotal">
          </el-pagination>
        </el-col>
      </el-row>
    </el-col>
  </el-row>
</template>

<script>
  import { listAssess ,getTAssessById,smm} from '@/api/xy/assess'
  import AssessEdit from "@/components/assess/assessEdit"
   import { listResourcePermission } from "@/api/resource"
  export default {
    data() {
      return {
        loading: false,
        permission: {
          add: true,
          edit: true,
          remove: true
        },
        search: {
          name: '',
          offset: 0,
          limit: 5,
          currentPage:1
        },
        assessTotal: 0,
        assessList: [],
      }
    },
    methods: {
      
      getAssessList() {
        this.loading = true
        listAssess(this.search)
          .then(response => {
            this.assessTotal = response.data.total
            this.assessList = response.data.rows
            this.loading = false
          })
          .catch(error => {
            console.log(error)
            this.loading = false
          })
        // alert(this.assessList)
      },
      handleSizeChange(val) {
        this.search.limit = val;
        this.search.offset = (this.search.currentPage - 1) * val
        this.getAssessList()
      },
      handleCurrentChange(val) {
        this.search.offset = (val - 1) * this.search.limit
        this.search.currentPage = val
        this.getAssessList()
      },
      async pageInit() {
        this.loading = true
        try {
<<<<<<< HEAD
          let [listAssessResp,listPermissionResp] = await Promise.all([
=======
          let [listAssessResp] = await Promise.all([
>>>>>>> origin/master
            listAssess(this.search),
            listResourcePermission(this.$route.meta.routerId)
          ])
          console.log(listAssessResp.data.rows)
          this.assessTotal = listAssessResp.data.total
          this.assessList = listAssessResp.data.rows

        //   this.permission.add = listPermissionResp.data.find(item => {
        //   return item.resourcePermission === "assess:add"
        // })
        // this.permission.edit = listPermissionResp.data.find(item => {
        //   return item.resourcePermission === "assess:update"
        // })
          this.loading = false
        } catch(error) {
          this.outputError(error)
        }
      },
      handleCreateAssess() {

        this.$refs.assessEdit.$emit('openAddAssessDialog')
      },
      handleEditAssess(index, row) {
        if(row.state == 1){
          this.loading = true;
          getTAssessById(row.memberId)
          .then(response => {
            this.$refs.assessEdit.$emit('openEditAssessDialog',response.data.tMember,row.memberId);
             this.loading = false;
            // console.log("----------obj:"+this.obj)
          })
          .catch(error => {
            this.outputError(error);
            this.loading = false;
          })
          smm(row.memberId).then(response=>{
            this.$refs.assessEdit.$emit('smm',response.data)
            this.loading = false;
          }).catch(error=>{
            this.outputError(error);
            this.loading = false;
          })
        }else{
          alert("您还未进行体检，请先体检。");
        }


      },
      outputError(error) {
        alert(error);
        console.log(error.response ? error.response : error.message)
        this.loading = false
        this.$message({
          showClose: true,
          message: '出错了，请按F12查看浏览器日志。',
          type: 'error'
        })
      }
    },
    components: { AssessEdit },
    created() {
      this.pageInit()
    }
  }
</script>

<style lang="scss" scoped>
  .search-row {
    padding-bottom: 8px;
    .el-input {
      width: 260px;
    }
  }

  .role-locked {
    color: red;
  }
  .role-not-locked {
    color: green;
  }
</style>
